<script setup>

import {onMounted, reactive, ref} from "vue";
import {findProductionSceduling} from "@/apis/zyj/ProductionSceduling.js";
import {Refresh, Search} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";

const props = defineProps({
  visible: Boolean,
})
const emit = defineEmits(['close', 'confirm'])

const tableData = reactive([])
// Pagination
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const selectWorkOrder = ref()

// Search form data
const searchForm = reactive({
  workorderCode: '',
  workorderName: '',
  sourceCode: '',
  productCode: '',
  productName: '',
  clientCode: '',
  clientName: '',
})

function initData() {
  searchForm.pageSize = pageSize.value
  searchForm.currentPage = currentPage.value
  findProductionSceduling(searchForm).then(res => {
    console.log("数据", res)
    tableData.length = 0
    Object.assign(tableData, res.records)
    total.value = res.total
    currentPage.value = res.current
    pageSize.value = res.size
  })
}

onMounted(() => {
  initData();
  console.log("数据", tableData)
})
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

function getOrderSource(a) {
  switch (a) {
    case 'ORDER':
      return '客户订单'
    case 'STORE':
      return '库存备货'
    default:
      return '未知'
  }
}

const handleSelectionChange = (val) => {
  selectWorkOrder.value = null
  selectWorkOrder.value = val
}
const handleSearch = () => {
  initData();
}
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
}
const handleClose = () => {
  emit('close')
}
const handleSelect = () => {
  console.log(selectWorkOrder.value)
  if (selectWorkOrder.value == null) {
    ElMessage.error("请选择数据")
    return;
  } else {
    emit('confirm', selectWorkOrder.value)
    handleClose()
  }
}
</script>
<template>
  <el-dialog v-model="props.visible" title="选择工单" width="80%" :before-close="handleClose" align-center>
    <div>
      <el-form :model="searchForm" inline class="mb-4">
        <el-form-item label="工单编码">
          <el-input v-model="searchForm.workorderCode" placeholder="请输入工单编码"/>
        </el-form-item>
        <el-form-item label="工单名称">
          <el-input v-model="searchForm.workorderName" placeholder="请输入工单名称"/>
        </el-form-item>
        <el-form-item label="产品编号">
          <el-input v-model="searchForm.productCode" placeholder="请输入产品编号"/>
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input v-model="searchForm.productName" placeholder="请输入产品名称"/>
        </el-form-item>
        <el-form-item label="客户编码">
          <el-input v-model="searchForm.clientCode" placeholder="请输入客户编码"/>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="searchForm.clientName" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-button type="primary" @click="handleSearch">
          <el-icon>
            <Search/>
          </el-icon>
          搜索
        </el-button>
        <el-button @click="handleReset">
          <el-icon>
            <Refresh/>
          </el-icon>
          重置
        </el-button>
      </el-form>
    </div>
    <div style="margin-top: 20px">
      <el-table :data="tableData" style="width: 100%" @current-change="handleSelectionChange" highlight-current-row>
        <el-table-column prop="workorderCode" label="工单编码" width="180"/>
        <el-table-column prop="workorderName" label="工单名称" width="180"/>
        <el-table-column prop="orderSource" label="工单来源" width="120">
          <template #default="scope">
            {{ getOrderSource(scope.row.orderSource) }}
          </template>
        </el-table-column>
        <el-table-column prop="clientOrderCode" label="订单编号" width="120"/>
        <el-table-column prop="productCode" label="产品编号" width="120"/>
        <el-table-column prop="productName" label="产品名称" width="150"/>
        <el-table-column prop="productSpc" label="规格型号" width="120"/>
        <el-table-column prop="unitOfMeasure" label="单位" width="80"/>
        <el-table-column prop="quantity" label="工单数量" width="100"/>
        <el-table-column prop="clientCode" label="客户编码" width="120"/>
        <el-table-column prop="clientName" label="客户名称" width="120"/>
        <el-table-column prop="requestDate" label="需求日期" width="120"/>
      </el-table>

      <!-- Pagination -->
      <div class="justify-between">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[5,10,20,25]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSelect">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.justify-between {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>